﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmBranchInfo : Form
    {
        bool isChange = false;
        BranchInfo _BranchInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        BranchClient wcfBranch;
        public frmBranchInfo(int BranchID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            // LoadLanguge();
            dlgResult = DialogResult.Cancel;
            wcfBranch = Service.Branch();
            _BranchInfo = new BranchInfo();

            _BranchInfo.ID = BranchID;
            if (BranchID > 0)
            {
                _BranchInfo = wcfBranch.Branch_GetInfo(BranchID);                
                Info2Form();
                txtCode.Enabled = false;
                txtBHXH.Enabled = false;
            }
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
            txtRegionCode.ReadOnly = (Program.isAdmin || (Program.strUserName.ToLower() == "vvtuy")) ? false : true;
            radBoth.Enabled = (Program.isAdmin || (Program.strUserName.ToLower() == "vvtuy")) ? true : false;
            radHS.Enabled = (Program.isAdmin || (Program.strUserName.ToLower() == "vvtuy")) ? true : false;
            radML.Enabled = (Program.isAdmin || (Program.strUserName.ToLower() == "vvtuy")) ? true : false;

        }
        void LoadLanguge()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        void Info2Form()
        {
            txtCode.Text = _BranchInfo.Code;
            txtAddress.Text = _BranchInfo.Address;
            txtPhone.Text = _BranchInfo.Phone;
            txtName.Text = _BranchInfo.Name;
            txtFax.Text = _BranchInfo.Fax;

            txtEmail.Text = _BranchInfo.Email;
            txtBranch_TaxCode.Text = _BranchInfo.Branch_TaxCode;
            txtBranch_BankAccount.Text = _BranchInfo.Branch_BankAccount;
            txtBranch_BankName.Text = _BranchInfo.Branch_BankName;

            txtScheduledMan.Text = _BranchInfo.Branch_ScheduledMan;
            txtBHXHOfficers.Text = _BranchInfo.Branch_BHXHOfficers;
            txtBHXHBossofProvince.Text = _BranchInfo.Branch_BHXHBossofProvince;
            txtBHXHBossofDistrict.Text = _BranchInfo.Branch_BHXHBossofDistrict;
            txtDescription.Text = _BranchInfo.Description;
            txtRepName.Text = _BranchInfo.RepName;
            txtRepPos.Text = _BranchInfo.RepPos;
            txtBHXH.Text = _BranchInfo.BHXH;
            txtCloseBook.Text = _BranchInfo.CloseBook;
            txtFamilySalary.Text = _BranchInfo.FamilySalary.ToString("#,0##");
            txtMinBasicSalary.Text = _BranchInfo.MinBasicSalary.ToString("#,0##");
            txtJoining.Text = _BranchInfo.Joining;
            txtManagementLevel.Text = _BranchInfo.ManagementLevel;
            txtMedicalInsurancePercent.Text = _BranchInfo.MedicalInsurancePercent.ToString(UIParamater.Cham2So);
            txtMedicalInsurancePercentLabour.Text = _BranchInfo.MedicalInsurancePercentLabour.ToString(UIParamater.Cham2So);
            txtSocialInsurancePercent.Text = _BranchInfo.SocialInsurancePercent.ToString(UIParamater.Cham2So);
            txtSocialInsurancePercentLabour.Text = _BranchInfo.SocialInsurancePercentLabour.ToString(UIParamater.Cham2So);
            txtTotalDateWork.Text = _BranchInfo.TotalDateWork;
            txtTradeUnion.Text = _BranchInfo.TradeUnion.ToString(UIParamater.Cham2So);
            txtUnemployedIns.Text = _BranchInfo.UnemployedIns.ToString(UIParamater.Cham2So);
            txtUnemployedInsCompany.Text = _BranchInfo.UnemployedInsCompany.ToString(UIParamater.Cham2So);
            txtRegionCode.Text = _BranchInfo.RegionCode;
            txtInsurancePlace.Text = _BranchInfo.InsurancePlace;
            lblInfo.Text = "Ngày tạo: " + _BranchInfo.Createdate.ToString("dd/MM/yyyy") + " - Ngày cập nhật cuối cùng: " + _BranchInfo.LastUpdate.ToString("dd/MM/yyyy");
            isChange = false;
            if (_BranchInfo.InsType == 0)
                radHS.Checked = true;
            else if (_BranchInfo.InsType == 1)
                radML.Checked = true;
            else if (_BranchInfo.InsType == 2)
                radBoth.Checked = true;
        }

        void Form2Info()
        {
            _BranchInfo.Code = txtCode.Text.Trim();
            _BranchInfo.Name = txtName.Text.Trim();
            _BranchInfo.Address = txtAddress.Text.Trim();
            _BranchInfo.Phone = txtPhone.Text.Trim();
            _BranchInfo.Fax = txtFax.Text.Trim();
            _BranchInfo.Email = txtEmail.Text.Trim();
            _BranchInfo.Branch_TaxCode = txtBranch_TaxCode.Text.Trim();
            _BranchInfo.Branch_BankAccount = txtBranch_BankAccount.Text;
            _BranchInfo.Branch_BankName = txtBranch_BankName.Text;

            _BranchInfo.Branch_ScheduledMan = txtScheduledMan.Text.Trim();
            _BranchInfo.Branch_BHXHOfficers = txtBHXHOfficers.Text.Trim();
            _BranchInfo.Branch_BHXHBossofProvince = txtBHXHBossofProvince.Text.Trim();
            _BranchInfo.Branch_BHXHBossofDistrict = txtBHXHBossofDistrict.Text.Trim();
            _BranchInfo.Description = txtDescription.Text.Trim();
            _BranchInfo.RepName = txtRepName.Text;
            _BranchInfo.RepPos = txtRepPos.Text;
            _BranchInfo.BHXH = txtBHXH.Text;
            _BranchInfo.CloseBook = txtCloseBook.Text;
            _BranchInfo.FamilySalary = Convert.ToDecimal(txtFamilySalary.Text);
            _BranchInfo.Joining = txtJoining.Text;
            _BranchInfo.ManagementLevel = txtManagementLevel.Text;
            _BranchInfo.MedicalInsurancePercent = Convert.ToDecimal(txtMedicalInsurancePercent.Text);
            _BranchInfo.MedicalInsurancePercentLabour = Convert.ToDecimal(txtMedicalInsurancePercentLabour.Text);
            _BranchInfo.MinBasicSalary = Convert.ToDecimal(txtMinBasicSalary.Text);
            _BranchInfo.SocialInsurancePercent = Convert.ToDecimal(txtSocialInsurancePercent.Text);
            _BranchInfo.SocialInsurancePercentLabour = Convert.ToDecimal(txtSocialInsurancePercentLabour.Text);
            _BranchInfo.TotalDateWork = txtTotalDateWork.Text;
            _BranchInfo.TradeUnion = Convert.ToDecimal(txtTradeUnion.Text);
            _BranchInfo.UnemployedIns = Convert.ToDecimal(txtTradeUnion.Text);
            _BranchInfo.UnemployedInsCompany = Convert.ToDecimal(txtUnemployedInsCompany.Text);
            _BranchInfo.RegionCode = txtRegionCode.Text;
            _BranchInfo.InsurancePlace = txtInsurancePlace.Text;
            if (radHS.Checked)
                _BranchInfo.InsType = 0;
            else if (radML.Checked)
                _BranchInfo.InsType = 1;
            else if (radBoth.Checked)
                _BranchInfo.InsType = 2;

        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (CheckTextValid.IsEmpty(txtCode.Text))
            {
                txtCode.Focus();
                MessageBox.Show("Chưa nhập mã số", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return false;
            }
            if (CheckTextValid.IsEmpty(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show("Chưa nhập tên", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (CheckTextValid.IsEmpty(txtAddress.Text))
            {
                txtAddress.Focus();
                MessageBox.Show("Chưa nhập địa chỉ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (CheckTextValid.IsEmpty(txtPhone.Text))
            {
                txtPhone.Focus();
                MessageBox.Show("Chưa nhập điện thoại", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (wcfBranch.Branch_Compare(_BranchInfo.ID, txtCode.Text))
            {
                MessageBox.Show("Mã chi nhánh đã tồn tại", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }
            if (_BranchInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    UISetting.MesNotRightEdit();
                    return;
                }
                if (wcfBranch.Branch_Update(_BranchInfo))
                {
                    UISetting.MesUpdateSuccesful();
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    UISetting.MesNotUpdate();
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    UISetting.MesNotRightNew();
                    return;
                }
                _BranchInfo.ID = wcfBranch.Branch_Insert(_BranchInfo);
                if (_BranchInfo.ID > 0)
                {
                    UISetting.MesCreateSuccesful();
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    UISetting.MesNotCreate();
                    isChange = true;
                }
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                UISetting.MesNotRightNew();
                return;
            }
            if (_BranchInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _BranchInfo.ID = 0;
            txtName.Text = "";
            txtCode.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtFax.Text = "";
            txtEmail.Text = txtBranch_TaxCode.Text = txtBranch_BankAccount.Text = txtBranch_BankName.Text = "";

            txtScheduledMan.Text = "";
            txtBHXHOfficers.Text = "";
            txtBHXHBossofProvince.Text = "";
            txtBHXHBossofDistrict.Text = "";

            txtDescription.Text = "";
            txtCloseBook.Text = "";
            txtFamilySalary.Text = "";
            txtJoining.Text = "";
            txtManagementLevel.Text = "";
            //txtMedicalInsurancePercent.Text = "";
            txtMedicalInsurancePercentLabour.Text = "";
            txtMinBasicSalary.Text = "";
            txtSocialInsurancePercent.Text = "";
            txtTotalDateWork.Text = "";
            txtTradeUnion.Text = "";
            txtRegionCode.Text = "";
        }

        private void frmBranchInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmBranchInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void txtRegionCode_Leave(object sender, EventArgs e)
        {
            RegionClient _wcfRegion = Service.Region();
            if (!_wcfRegion.Region_Exist(txtRegionCode.Text.Trim()))
            {
                frmRegionList f = new frmRegionList(true);
                if (f.ShowDialog(this) == DialogResult.OK)
                    txtRegionCode.Text = f.RegionCode;
            }
        }

      
    }
}
